package com.sv.jm.baselibrary.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/27.
 */

public abstract class SuperAdapter<T> extends BaseAdapter {
    public List<T> datas = new ArrayList<>();
    int resid;
    Context mContext;
    public SuperAdapter(int resid, Context mContext) {
        this.resid = resid;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    public void setDatas(List<T> datas) {
        if (null != datas)
            this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearnDatas() {
        datas.clear();
        notifyDataSetChanged();
    }

    public void addDatas(List<T> datas) {
        if (null != datas)
            this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder holder = null;
        if (null == convertView) {
            convertView = View.inflate(mContext, resid, null);
            holder = getViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (BaseViewHolder) convertView.getTag();
        }
        onViewCreated(holder,position);
        return convertView;
    }

    public abstract void  onViewCreated(BaseViewHolder holder, int position);

    public abstract BaseViewHolder getViewHolder(View v);
}
